package com.cos.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.util.JSONPObject;
/**
 * App 工具类
 * @author ：
 *
 */
public class AppUtil  {
	
	protected static Logger logger = Logger.getLogger(AppUtil.class);  //获取日志对象
	
	public static void main(String[] args) {
		PageData pd = new PageData();
		pd.put("username", "zhangsan");
		
		checkParam("registered", pd);
	}
	//检查参数是否完整
	public static boolean checkParam(String method, PageData pd){
		boolean result = false;
		
		int falseCount = 0;
		String[] paramArray = new String[20];
		String[] valueArray = new String[20];
		String[] tempArray  = new String[20];  //临时数组
		
		if(method=="registered"){// 注册
			paramArray = Const.APP_REGISTERED_PARAM_ARRAY;  //参数
			valueArray = Const.APP_REGISTERED_VALUE_ARRAY;  //参数名称
			
		}else if(method=="login"){  //登录
			paramArray = Const.APP_LOGIN_PARAM_ARRAY;  //参数
			valueArray = Const.APP_LOGIN_VALUE_ARRAY;  //参数名称
		}
		else if(method=="getAppuserByUsernmae"){//根据用户名获取会员信息
			paramArray = Const.APP_GETAPPUSER_PARAM_ARRAY;  
			valueArray = Const.APP_GETAPPUSER_VALUE_ARRAY;
		}else if(method=="getGroup"){//根据用户ID 获取用户拥有的好友  及其组别
			paramArray = Const.APP_GETGROUP_PARAM_ARRAY;
			valueArray = Const.APP_GETGROUP_VALUE_ARRAY;
		}else if(method=="getDepartment"){//获取部门
			paramArray = Const.APP_GETDEPARMENT_PARAM_ARRAY;  
			valueArray = Const.APP_GETDEPARMENT_VALUE_ARRAY;
		}else if(method == "getWordAdviceByDID"){//获取工作提议
			paramArray = Const.APP_GETWORKADVICE_PARAM_ARRAY;  
			valueArray = Const.APP_GETWORKADVICE_VALUE_ARRAY;
		}else if(method== "getNoteCompany"){//获取同事录
			paramArray = Const.APP_GETNOTE_PARAM_ARRAY;  
			valueArray = Const.APP_GETNOTE_VALUE_ARRAY;
		}else if(method=="getWorkAdviceID"){//获取工作提议的详情
			paramArray = Const.APP_WORKADVICE_PARAM_ARRAY;  
			valueArray = Const.APP_WORKADVICE_VALUE_ARRAY;
		}else if(method=="getComment"){ //获取评论
			paramArray = Const.APP_COMMENT_PARAM_ARRAY;  
			valueArray = Const.APP_COMMENT_VALUE_ARRAY;
		}else if(method=="getReply"){//获取回复
			paramArray = Const.APP_REPLY_PARAM_ARRAY;  
			valueArray = Const.APP_REPLY_VALUE_ARRAY;
		}else if(method=="getPlanComment"){//工作痕迹评论 
			paramArray = Const.APP_PLANCOMMENT_PARAM_ARRAY;  
			valueArray = Const.APP_PLANCOMMENT_VALUE_ARRAY;
		}else if(method=="saveWordAdvice"){//发布工作提议
			paramArray = Const.APP_SAVEWORKADVICE_PARAM_ARRAY;  
			valueArray = Const.APP_SAVEWORKADVICE_VALUE_ARRAY;
		}else if(method=="getCompany"){//创建公司
			paramArray = Const.APP_GETCOMPANY_PARAM_ARRAY;  
			valueArray = Const.APP_GETCOMPANY_VALUE_ARRAY;
		}else if(method=="getUpdateCompany"){//更新公司Logo
			paramArray = Const.APP_GETUPDATECOMPANY_PARAM_ARRAY;  
			valueArray = Const.APP_GETUPDATECOMPANY_VALUE_ARRAY;
		}else if(method=="saveMemoryTag"){//新增备忘录标签
			paramArray = Const.APP_MEMORYTAG_PARAM_ARRAY;  
			valueArray = Const.APP_MEMORYTAG__VALUE_ARRAY;
		}else if(method=="saveMemory"){//新增备忘录
			paramArray = Const.APP_MEMORY_PARAM_ARRAY;  
			valueArray = Const.APP_MEMORY__VALUE_ARRAY;
		}else if(method=="saveWorkRecommend"){//新增工作提议
			paramArray = Const.APP_WORKRECOMMEND_PARAM_ARRAY;  
			valueArray = Const.APP_WORKRECOMMEND_VALUE_ARRAY;
		}else if(method=="saveQaRecommend"){//新增主管通知
			paramArray = Const.APP_RECOMMEND_PARAM_ARRAY;  
			valueArray = Const.APP_RECOMMEND_VALUE_ARRAY;
		}else if(method=="getWorkPlanByUID"){//获取工作分工
			paramArray = Const.APP_WORKPLAN_PARAM_ARRAY;  
			valueArray = Const.APP_WORKPLAN_VALUE_ARRAY;
		}else if(method=="getWordPlanByDID"){//获取工作分工
			paramArray = Const.APP_WORKPLANT_PARAM_ARRAY;  
			valueArray = Const.APP_WORKPLANT_VALUE_ARRAY;
		}else if(method=="getMemory"){//获取备忘录
			paramArray = Const.APP_GETMEMORY_PARAM_ARRAY;  
			valueArray = Const.APP_GETMEMORY_VALUE_ARRAY;
		}else if(method=="getCName"){//更新公司名字
			paramArray = Const.APP_GETCName_PARAM_ARRAY;  
			valueArray = Const.APP_GETCName_VALUE_ARRAY;
		}else if(method == "saveComment"){//新增用户评论
			paramArray = Const.APP_SAVECOMMENT_PARAM_ARRAY;
			valueArray = Const.APP_SAVECOMMENT_VALUE_ARRAY;
		}else if(method =="saveReply"){//新增用户评论回复
			paramArray = Const.APP_SAVEREPLY_PARAM_ARRAY;
			valueArray = Const.APP_SAVEREPLY_VALUE_ARRAY;
		}else if(method == "saveComment"){//新增用户评论
			paramArray = Const.APP_SAVECOMMENT_PARAM_ARRAY  ;
			valueArray = Const.APP_SAVECOMMENT_VALUE_ARRAY ;
		}else if(method =="saveReply"){//新增用户评论回复
			paramArray = Const.APP_SAVEREPLY_PARAM_ARRAY ;
			valueArray = Const.APP_SAVEREPLY_VALUE_ARRAY ;
		}else if(method=="saveWordPLAN"){//发布工作分工
			paramArray = Const.APP_SAVEWORKPLAN_PARAM_ARRAY;  
			valueArray = Const.APP_SAVEWORKPLAN_VALUE_ARRAY;
		}else if(method == "saveSupport"){//新增动态赞
			paramArray = Const.APP_SAVESUPPORT_PARAM_ARRAY ;
			valueArray = Const.APP_SAVESUPPORT_VALUE_ARRAY ;
		}else if(method == "saveTrample"){//新增踩赞
			paramArray = Const.APP_SAVETRAMPLE_PARAM_ARRAY ;
			valueArray = Const.APP_SAVETRAMPLE_VALUE_ARRAY ;
		}else if(method =="saveAppMenu"){//新增用户评论回复
			paramArray = Const.APP_SAVEAPPMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SAVEAPPMENU_VALUE_ARRAY ;
		}else if(method=="getCName"){//更新公司名字
			paramArray = Const.APP_GETCName_PARAM_ARRAY;  
			valueArray = Const.APP_GETCName_VALUE_ARRAY;
		}else if(method=="saveWordPLAN"){//发布工作分工
			paramArray = Const.APP_SAVEWORKPLAN_PARAM_ARRAY;  
			valueArray = Const.APP_SAVEWORKPLAN_VALUE_ARRAY;
		}else if(method == "saveSupport"){//新增动态赞
			paramArray = Const.APP_SAVESUPPORT_PARAM_ARRAY ;
			valueArray = Const.APP_SAVESUPPORT_VALUE_ARRAY ;
		}else if(method == "saveTrample"){
			paramArray = Const.APP_SAVETRAMPLE_PARAM_ARRAY ;
			valueArray = Const.APP_SAVETRAMPLE_VALUE_ARRAY ;
		}else if(method=="saveUserMenu"){ //我的里面更多功能里面去新增功能
			paramArray = Const.APP_SAVEUSERMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SAVEUSERMENU_VALUE_ARRAY ;
		}else if(method=="saveCompanyMenu"){//公司里面的功能里面去新增功能
			paramArray = Const.APP_SAVECOMPANYMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SAVECOMPANYMENU_VALUE_ARRAY ;
		}else if(method=="searchCompany"){//查找公司
			paramArray = Const.APP_SEARCHCOMPANYMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SEARCHCOMPANYMENU_VALUE_ARRAY ;
		}else if(method=="joinCompany"){//会员加入公司
			paramArray = Const.APP_JOINCOMPANYMENU_PARAM_ARRAY ;
			valueArray = Const.APP_JOINCHCOMPANYMENU_VALUE_ARRAY ;
		}else if(method=="saveDepartment"){//创建团组
			paramArray = Const.APP_SAVEDEPARTMENTMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SAVEDEPARTMENTMENU_VALUE_ARRAY ;
		}else if(method=="saveSonDepartment"){//创建子类部门
			paramArray = Const.APP_SAVESONDEPARTMENTMENU_PARAM_ARRAY ;
			valueArray = Const.APP_SAVESONDEPARTMENTMENU_VALUE_ARRAY ;
		}else if(method=="joinDepartment"){//公司将员工加入部门
			paramArray = Const.APP_JOINDEPARTMENTMENU_PARAM_ARRAY ;
			valueArray = Const.APP_JOINDEPARTMENTMENU_VALUE_ARRAY ;
		}else if(method=="saveCompanyCollection"){//新增收藏动态
			paramArray = Const.APP_SAVECOMPANYCOLLECTION_PARAM_ARRAY ;
			valueArray = Const.APP_SAVECOMPANYCOLLECTION_VALUE_ARRAY ;
		}else if(method=="getCompanyType"){//根据类别获取公司
			paramArray = Const.APP_GETCOMPANYTYPE_PARAM_ARRAY ;
			valueArray = Const.APP_GETCOMPANYTYPE_VALUE_ARRAY ;
		}else if(method=="getCompanyCustom"){//获取我的定制
			paramArray = Const.APP_GETCOMPANYCUSTOM_PARAM_ARRAY ;
			valueArray = Const.APP_GETCOMPANYCUSTOM_VALUE_ARRAY ;
		}else if(method=="selectAttachment"){//获取附近公司
			paramArray = Const.APP_SELECTATTACHMENT_PARAM_ARRAY ;
			valueArray = Const.APP_SELECTATTACHMENT_VALUE_ARRAY ;
		}
		
		int size = paramArray.length;
		for(int i=0;i<size;i++){
			String param = paramArray[i];
			if(!pd.containsKey(param)){
				tempArray[falseCount] = valueArray[i]+"--"+param;
				falseCount += 1;
			}
		}
		
		if(falseCount>0){
			logger.error(method+"接口，请求协议中缺少 "+falseCount+"个 参数");
			for(int j=1;j<=falseCount;j++){
				logger.error("   第"+j+"个："+ tempArray[j-1]);
			}
		}else {
			result = true;
		}
		return result  ;
	}
	
	/**
	 * 设置分页的参数
	 * @param pd
	 * @return
	 */
	public static PageData setPageParam(PageData pd){
		String page_now_str = pd.get("page_now").toString();
		int pageNowInt = Integer.parseInt(page_now_str)-1;
		String page_size_str = pd.get("page_size").toString(); //每页显示记录数
		int pageSizeInt = Integer.parseInt(page_size_str);
		
		String page_now = pageNowInt+"";
		String page_start = (pageNowInt*pageSizeInt)+"";
		
		pd.put("page_now", page_now);
		pd.put("page_start", page_start);
		
		return pd;
	}
	
	/**
	 * 设置list中的distance(间隔)
	 */
	public static List<PageData>  setListDistance(List<PageData> list, PageData pd){
		List<PageData> listReturn = new ArrayList<PageData>();
		String user_longitude = "";
		String user_latitude = "";
		
		try{
			user_longitude = pd.get("user_longitude").toString(); //"117.11811";
			user_latitude  = pd.get("user_latitude").toString();  //"36.68484";
		} catch(Exception e){
			logger.error("缺失参数--user_longitude和user_longitude");
			logger.error("lost param：user_longitude and user_longitude");
		}
		
		PageData pdTemp = new PageData();
		int size = list.size();
		for(int i=0;i<size;i++){
			pdTemp = list.get(i);
			String longitude = pdTemp.get("longitude").toString();
			String latitude = pdTemp.get("latitude").toString();
			String distance = MapDistance.getDistance(
						user_longitude,	user_latitude,
						longitude,		latitude
					);
			pdTemp.put("distance", distance);
			pdTemp.put("size", distance.length());
			listReturn.add(pdTemp);
		}
		
		return listReturn;
	}
	
	public static Object returnObject(PageData pd, Map map){
		if(pd.containsKey("callback")){
			String callback = pd.get("callback").toString();
			return new JSONPObject(callback, map);
		}else{
			return map;
		}
	}
}
